package com.aloda.controller.admin;

import com.aloda.anno.OpLog;
import com.aloda.dto.*;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.EmpService;
import com.aloda.vo.AuthVO;
import com.aloda.vo.EmployeeEditVO;
import com.aloda.vo.EmployeeLoginVO;
import com.aloda.vo.EmployeeSetVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/emp")
@Slf4j
@Api(tags = "员工相关接口")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 测试接口
     * @param username
     * @return
     */
    @PostMapping("/test")
    @ApiOperation("测试接口")
    public Result test(String username){
        log.info("username：{}",username);
        return Result.success();
    }

    /**
     * 员工后台登录
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录接口")
    public Result<EmployeeLoginVO> empLogin(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        log.info("登录员工：{}",employeeLoginDTO.getUsername());
        EmployeeLoginVO employeeLoginVO = empService.empLogin(employeeLoginDTO);
        log.info("返回登录消息：{}",employeeLoginVO);
        return Result.success(employeeLoginVO);
    }

    /**
     * 当前登录者的权限
     * TODO 接收id但不使用，利用线程和jwt方法取出当前登录用户id,两者可以比较
     * @return
     */
    @GetMapping("/auth")
    @ApiOperation("查询登录者权限接口")
    public Result<AuthVO> empAuth(Long id){
        log.info("后台登录者{}申请权限",id);
        AuthVO authVO = empService.getEmpAuth(id);
        return Result.success(authVO);
    }

    /**
     * 个人信息设置回显
     * @return
     */
    @GetMapping("/set")
    public Result<EmployeeSetVO> empSetInfo(){
        log.info("信息回显{}",1);
        EmployeeSetVO employeeSetVO = empService.getEmpSetInfo();
        return Result.success(employeeSetVO);
    }

    /**
     * 员工修改个人信息
     * @param employeeModifyDTO
     * @return
     */
    @OpLog
    @PostMapping("/modify")
    public Result empModifyInfo(@RequestBody EmployeeModifyDTO employeeModifyDTO){
        log.info("修改个人信息：{}",employeeModifyDTO);
        empService.empModifyInfo(employeeModifyDTO);
        return Result.success();
    }

    /**
     * 员工修改密码
     * @param modifyPwdDTO
     * @return
     */
    @PostMapping("/password")
    public Result empModifyPwd(@RequestBody ModifyPwdDTO modifyPwdDTO){
        empService.empModifyPwd(modifyPwdDTO);
        return Result.success();
    }

    /**
     * 员工分页查询
     * @param employeePageDTO
     * @return
     */
    @PostMapping("/list")
    public Result<PageResult> empPage(@RequestBody EmployeePageDTO employeePageDTO){
        log.info("员工分页查询：{}",employeePageDTO);
        PageResult pageResult = empService.empPage(employeePageDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增员工
     * @param employeeAddDTO
     * @return
     */
    @OpLog
    @PostMapping("/add")
    public Result add(@RequestBody EmployeeAddDTO employeeAddDTO){
        log.info("新增员工：{}",employeeAddDTO);
        empService.addEmp(employeeAddDTO);
        return Result.success();
    }

    /**
     * 根据id回显编译员工页面的数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<EmployeeEditVO> getEmpById(@PathVariable Long id){
        log.info("编辑员工{}回显",id);
        EmployeeEditVO employeeEditVO = empService.getEmpById(id);
        return Result.success(employeeEditVO);
    }


    /**
     * 编辑员工信息
     * @param employeeEditDTO
     * @return
     */
    @OpLog
    @PutMapping("/update")
    public Result updateEmp(@RequestBody EmployeeEditDTO employeeEditDTO){
        log.info("编辑员工信息{}",employeeEditDTO);
        empService.updateEmp(employeeEditDTO);
        return Result.success();
    }

    /**
     * 员工重置密码
     * @param id
     * @return
     */
    @PutMapping("/resetPwd/{id}")
    public Result resetPwd(@PathVariable Long id){
        log.info("员工{}重置密码",id);
        empService.resetPwd(id);
        return Result.success();
    }

    /**
     * 设置员工账号状态
     * @param id
     * @param status
     * @return
     */
    @OpLog
    @PutMapping("/resetStatus/{id}/{status}")
    public Result startOrStop(@PathVariable Long id,@PathVariable Integer status){
        log.info("员工{}状态设为{}",id,status);
        empService.resetStatus(id,status);
        return Result.success();
    }

    /**
     * 设置员工注销状态
     * @param id
     * @param auth
     * @return
     */
    @OpLog
    @PutMapping("/logout/{id}/{auth}")
    public Result logout(@PathVariable Long id,@PathVariable Integer auth){
        log.info("员工{}权限设为{}",id,auth);
        empService.logoutEmp(id,auth);
        return Result.success();
    }

}
